package com.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 服务器端的初始化和关闭
 */
public class ServerInitClose {
    // 定义ServerSocket对象
    private ServerSocket serverSocket;
    // 定义Socket对象
    private Socket socket;
    // 定义输入输出流
    private ObjectInputStream objectInputStream;
    private ObjectOutputStream objectOutputStream;

    public ObjectInputStream getObjectInputStream() {
        return objectInputStream;
    }

    public ObjectOutputStream getObjectOutputStream() {
        return objectOutputStream;
    }

    /**
     * 服务器初始化
     */
    public void serverInit() throws IOException {
        // 创建端口号为9001的服务器
        serverSocket = new ServerSocket(9001);
        System.out.println("等待客户机连接中。。。。。。");
        socket = serverSocket.accept();
        System.out.println("客户端连接成功！");
        objectInputStream = new ObjectInputStream(socket.getInputStream());
        objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("服务器初始化成功！");
    }

    /**
     * 服务器关闭
     */
    public void serverClose() throws IOException {
        objectOutputStream.close();
        objectInputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务器关闭成功！");
    }
}
